Generate complex password

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Paolo Frigo, 2018 
# https://www.scriptinglibrary.com

Function New-RandomPassword{
    Param(
        [ValidateRange(8, 32)]
        [int] $Length = 16
    )
    $AsciiCharsList = @()
    foreach ($a in (33..126)){
        $AsciiCharsList += , [char][byte]$a 
    }
    #RegEx for checking general AD Complex passwords
    $RegEx = "(?=^.{8,32}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*"

    do {
        $Password = ""
        $loops = 1..$Length
        Foreach ($loop in $loops) {
            $Password += $AsciiCharsList | Get-Random
        }
    }
    until ($Password -match $RegEx )
    return $Password
}

# This example just print a random password
# Write-Output "Generate Random Password: $(New-RandomPassword(10))"